local skel = fk.CreateSkill {
  name = "lb__tingchen",
}

Fk:loadTranslationTable {
  ["lb__tingchen"] = "霆尘",
  [":lb__tingchen"] = "出牌阶段限一次，你可以令一名角色对你使用一张【杀】，若你未因此受到伤害，你可摸两张牌并令与其相邻的任意名角色横置，然后其进行一次【闪电】判定。",

  ["#lb__tingchen"] = "霆尘：你可以令一名角色对你使用一张【杀】",
  ["#lb__tingchen_slash"] = "霆尘：请对 %src 使用一张【杀】",
  ["#lb__tingchen_darw"] = "霆尘：你可以摸两张牌并令与 %src 相邻的任意名角色横置，然后 %src 进行一次【闪电】判定",
  ["#lb__tingchen_chain"] = "霆尘：你可以令与 %src 相邻的任意名角色横置，然后 %src 进行一次【闪电】判定",
}

skel:addEffect("active", {
  prompt = "#lb__tingchen",
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards, card, extra_data)
    return #selected == 0
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) == 0
  end,
  feasible = function(self, player, selected, selected_cards, card)
    return #selected == 1
  end,
  on_use = function(self, room, skillUseEvent)
    local player = skillUseEvent.from
    local target = skillUseEvent.tos[1]
    local use = room:askToUseCard(target, {
      skill_name = "slash",
      pattern = "slash",
      prompt = "#lb__tingchen_slash:" .. player.id,
      cancelable = true,
      extra_data = {
        must_targets = { player.id },
        bypass_times = true,
      }
    })
    if use then
      use.extraUse = true
      room:useCard(use)
    end
    if not (use and use.damageDealt and use.damageDealt[player]) then
      if room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#lb__tingchen_darw:" .. target.id }) then
        player:drawCards(2, skel.name)
        local tos = { target:getNextAlive() }
        table.insertIfNeed(tos, target:getLastAlive())
        tos = table.filter(tos, function(to)
          return not to.chained
        end)
        if #tos > 0 then
          tos = room:askToChoosePlayers(player, {
            targets = tos,
            min_num = 1,
            max_num = #tos,
            skill_name = skel.name,
            cancelable = true,
            prompt = "#lb__tingchen_chain:" .. target.id,
          })
          for _, to in ipairs(tos) do
            if to:isAlive() and not to.chained then
              to:setChainState(true)
            end
          end
        end
        if target:isAlive() then
          local pattern = ".|2~9|spade"
          local judge = {
            who = target,
            reason = "lightning",
            pattern = pattern,
          }
          room:judge(judge)
          if judge:matchPattern() then
            room:damage {
              to = target,
              damage = 3,
              damageType = fk.ThunderDamage,
              skillName = skel.name,
            }
          end
        end
      end
    end
  end,
})

return skel